﻿namespace ExamV5.ManagSys
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmSelectDept : Page
    {
        protected Button butAllDel;
        protected Button butAllSelect;
        protected Button butOneDel;
        protected Button butOneSelect;
        protected Button butQuery;
        protected HtmlForm frmuser;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        protected Label labSelectedTip;
        protected Label labSelectTip;
        protected ListBox LBSelect;
        protected ListBox LBSelected;
        private PublicFun publicFun_0 = new PublicFun();
        private string string_0 = "";
        protected TextBox txtQuery;

        protected void butAllDel_Click(object sender, EventArgs e)
        {
            ListItem item = null;
            for (int i = 0; i < this.LBSelected.Items.Count; i++)
            {
                item = new ListItem(this.LBSelected.Items[i].Text, this.LBSelected.Items[i].Value);
                if (this.LBSelect.Items.IndexOf(item) == -1)
                {
                    this.LBSelect.Items.Add(item);
                }
            }
            this.LBSelected.Items.Clear();
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }

        protected void butAllSelect_Click(object sender, EventArgs e)
        {
            int num;
            for (num = 0; num < this.LBSelected.Items.Count; num++)
            {
                if (this.LBSelected.Items[num].Text == "所有部门")
                {
                    base.Response.Write("<script>alert('已选择所有部门,不用再选择！')</script>");
                    return;
                }
            }
            ListItem item = null;
            for (num = 0; num < this.LBSelect.Items.Count; num++)
            {
                item = new ListItem(this.LBSelect.Items[num].Text, this.LBSelect.Items[num].Value);
                if (this.LBSelected.Items.IndexOf(item) == -1)
                {
                    this.LBSelected.Items.Add(item);
                }
            }
            this.LBSelect.Items.Clear();
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }

        protected void butOneDel_Click(object sender, EventArgs e)
        {
            string str = "";
            if (this.LBSelected.SelectedIndex > -1)
            {
                int num2 = 0;
                while (num2 < this.LBSelected.Items.Count)
                {
                    if (this.LBSelected.Items[num2].Selected)
                    {
                        if (this.LBSelect.Items.IndexOf(this.LBSelected.Items[num2]) == -1)
                        {
                            this.LBSelect.Items.Add(this.LBSelected.Items[num2]);
                        }
                        str = str + this.LBSelected.Items[num2].Value + "#";
                    }
                    num2++;
                }
                string[] strArray = str.Split(new char[] { '#' });
                if (strArray.Length > 1)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        for (num2 = 0; num2 < this.LBSelected.Items.Count; num2++)
                        {
                            if (this.LBSelected.Items[num2].Value == strArray[i])
                            {
                                this.LBSelected.Items.Remove(this.LBSelected.Items[num2]);
                            }
                        }
                    }
                }
            }
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }

        protected void butOneSelect_Click(object sender, EventArgs e)
        {
            int num;
            for (num = 0; num < this.LBSelected.Items.Count; num++)
            {
                if (this.LBSelected.Items[num].Text == "所有部门")
                {
                    base.Response.Write("<script>alert('已选择所有部门,不用再选择！')</script>");
                    return;
                }
            }
            string str = "";
            if (this.LBSelect.SelectedIndex > -1)
            {
                num = 0;
                while (num < this.LBSelect.Items.Count)
                {
                    if (this.LBSelect.Items[num].Selected && (this.LBSelected.Items.IndexOf(this.LBSelect.Items[num]) == -1))
                    {
                        this.LBSelected.Items.Add(this.LBSelect.Items[num]);
                        str = str + this.LBSelect.Items[num].Value + "#";
                    }
                    num++;
                }
                string[] strArray = str.Split(new char[] { '#' });
                if (strArray.Length > 1)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        for (num = 0; num < this.LBSelect.Items.Count; num++)
                        {
                            if (this.LBSelect.Items[num].Value == strArray[i])
                            {
                                this.LBSelect.Items.Remove(this.LBSelect.Items[num]);
                            }
                        }
                    }
                }
            }
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.method_0();
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }

        private void method_0()
        {
            string cmdText = "";
            if (!(this.httpCookie_2.Value == "C"))
            {
                cmdText = "Select ID,DeptName From DeptInfo where ID=" + this.httpCookie_3.Value + " ";
            }
            else
            {
                cmdText = "Select ID,DeptName From DeptInfo Where UpID=0 ";
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                cmdText = cmdText + " and DeptName like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            cmdText = cmdText + " order by UPID,SortInfo asc ";
            this.LBSelect.Items.Clear();
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(cmdText, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                ListItem item2 = new ListItem(reader["DeptName"].ToString().Replace(",", "，"), reader["ID"].ToString());
                this.LBSelect.Items.Add(item2);
                this.method_1(reader["ID"].ToString(), 1);
            }
            ListItem item = new ListItem("所有部门", "0");
            this.LBSelect.Items.Add(item);
            reader.Close();
            connection.Close();
            command.Dispose();
            connection.Dispose();
        }

        private void method_1(string string_1, int int_1)
        {
            string str = "";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            string str2 = "Select ID,DeptName From DeptInfo Where UpID = " + string_1 + " ";
            if (this.txtQuery.Text.Trim() != "")
            {
                str2 = str2 + " and DeptName like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            SqlCommand command = new SqlCommand(str2 + " order by UPID,SortInfo ", connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                str = "";
                for (int i = 0; i < int_1; i++)
                {
                    if (i >= (int_1 - 1))
                    {
                        str = str + "├";
                    }
                    else
                    {
                        str = str + "│";
                    }
                }
                ListItem item = new ListItem(str + reader["DeptName"].ToString().Replace(",", "，"), reader["ID"].ToString());
                this.LBSelect.Items.Add(item);
                this.method_1(reader["ID"].ToString(), int_1 + 1);
            }
            reader.Close();
            connection.Close();
            command.Dispose();
            connection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection;
            string str4;
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 == null) || (this.httpCookie_0.Value == ""))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
                return;
            }
            base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            base.Response.Cache.SetNoStore();
            this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
            this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
            this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
            this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
            if (base.Request["ID"] == null)
            {
                this.int_0 = 0;
            }
            else
            {
                this.int_0 = Convert.ToInt32(base.Request["ID"].ToString());
            }
            if (base.Request["Flag"] != null)
            {
                this.string_0 = base.Request["Flag"].ToString();
            }
            else
            {
                this.string_0 = "";
            }
            if (base.IsPostBack)
            {
                return;
            }
            string str = "";
            string str2 = "";
            string str5 = this.string_0.Trim();
            switch (str5)
            {
                case null:
                    break;

                case "S":
                    str4 = "select BrowDeptName,BrowDeptID from SubjectInfo ";
                    goto Label_029C;

                default:
                    if (!(str5 == "T"))
                    {
                        if (!(str5 == "G"))
                        {
                            break;
                        }
                        str4 = "select BrowDeptName,BrowDeptID from GradeFolder ";
                    }
                    else
                    {
                        str4 = "select BrowDeptName,BrowDeptID from TypeList ";
                    }
                    goto Label_029C;
            }
            str4 = "select BrowDeptName,BrowDeptID from TypeList ";
        Label_029C:
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(str4 + " where ID=" + this.int_0, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                str = reader["BrowDeptID"].ToString();
                str2 = reader["BrowDeptName"].ToString();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            string[] strArray = str.Split(new char[] { ',' });
            string[] strArray2 = str2.Split(new char[] { ',' });
            this.LBSelected.Items.Clear();
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i].Trim().Trim() != "")
                {
                    ListItem item = new ListItem(strArray2[i].Trim(), strArray[i].Trim());
                    this.LBSelected.Items.Add(item);
                }
            }
            this.method_0();
            this.labSelectTip.Text = "待选单位部门<font color=red>(" + this.LBSelect.Items.Count + ")</font>";
            this.labSelectedTip.Text = "待选单位部门<font color=red>(" + this.LBSelected.Items.Count + ")</font>";
        }
    }
}

